package com.yuandian.business.monitor.controller;

import cn.hutool.json.JSONUtil;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.business.monitor.service.IDocumentExportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 文档导出请求处理，控制中心
 * @author User
 *
 */
@Slf4j
@RestController
@RequestMapping("/document/export")
public class DocumentExportController {

	@Autowired
	private IDocumentExportService documentExportService;

	/**
	 *  3、下载文件，(支持下载文件后，删除文件)
	 * @param fileName
	 * @param response
	 */
	@GetMapping("/downloadDocs")
	public void downloadDoc(String fileName, HttpServletResponse response){
		try {
			documentExportService.downloadDoc(fileName, response);
		} catch (Exception e) {
			setResponseResult(R.failed("导出Excel文件失败！"), response);
		}
	}
	/**
	 * 设置通用返回对象到响应上下文
	 * @param result 通用返回对象
	 * @param response 响应上下文
	 * @throws IOException
	 */
	public  void setResponseResult(R result, HttpServletResponse response) {
		response.reset();
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		try {
			response.getWriter().println(JSONUtil.parse(result));
		} catch (IOException e) {
			log.error("",e);
		}
	}


}
